<?php

include("connect.php");
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
function getFuneralHomeID($name, $dblink) {


$query = sprintf("SELECT `ID` FROM FuneralHome WHERE `Name` = '%s'", mysql_real_escape_string($name));

	 $result = mysql_query($query, $dblink);



	// Check result
	// This shows the actual query sent to MySQL, and the error. Useful for debugging.
	if (!$result) {
		$message  = 'Invalid query: ' . mysql_error() . "\n";
	    	$message .= 'Whole query: ' . $query;
    		die($message);
	}
	$value = "";
	if(mysql_num_rows($result)!=0) {
		$value = mysql_result($result, 0);	
	}
	return $value;
}

function insertNewFuneralHome($name, $dblink) {
	


	$blah = getFuneralHomeID($name, $dblink);

	if($blah != "" && $blah != null) {
		return $blah;
	} else {

	$query = sprintf("INSERT INTO FuneralHome (`Name`) VALUES ('%s')", 
				mysql_real_escape_string($name));

	 $result = mysql_query($query, $dblink);

	// Check result
	// This shows the actual query sent to MySQL, and the error. Useful for debugging.
	if (!$result) {
		$message  = 'Invalid query: ' . mysql_error() . "\n";
	    	$message .= 'Whole query: ' . $query;
    		die($message);
	}

	return  mysql_insert_id();
}
}

if($_POST["name"] != "" && $_POST["name"] != NULL) {
$data = $_POST["name"];


$funeralhomeid = insertNewFuneralHome($data, $link);

echo $funeralhomeid;

}

mysql_close($link);
?>
